package demo;

import java.util.List;

import cn.wanghaomiao.seimi.annotation.Crawler;
import cn.wanghaomiao.seimi.def.BaseSeimiCrawler;
import cn.wanghaomiao.seimi.struct.Request;
import cn.wanghaomiao.seimi.struct.Response;
import cn.wanghaomiao.xpath.model.JXDocument;

@Crawler(name = "basic")
public class Basic extends BaseSeimiCrawler{

	@Override
	public void start(Response response) {
		JXDocument doc = response.document();
		try {
			String selstr = "//a[@class='titlelnk']/@href";
			List<Object> urls = doc.sel(selstr);
			logger.info("{}", urls.size());
			for (Object s : urls){
				push(new Request(s.toString(), "getTitle"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String[] startUrls() {
		return new String[]{"http://www.cnblogs.com/"};
	}

	public void getTitle(Response response){
		JXDocument doc = response.document();
		String selstr = "//h1[@class='postTitle']/a/text()|//a[@id='cb_post_title_url']/text()";
		try {
			logger.info("url:{} {}", response.getUrl(), doc.sel(selstr));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
